home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / scrub.c < prev    next >
Text File  |  1990-07-30  |  4KB  |  143 lines

  1. /*
  2.     Program to "scrub" a WordStar text file back to a
  3.     standard ASCII file.
  4.  
  5. VERSION LIST, most recent version first
  6.  
  7. 26/Sep/82
  8.     Forces MSB of all characters to 0, then scans for control
  9.     codes. TAB, CR and LF are passed unchanged to the output
  10.     file. US (soft hyphen) is replaced by a hard hyphen.
  11.     Checking for legal CP/M filename on destination file
  12.     added. Expanded "usage" message. Added "working" messages.
  13.     Bill Bolton. 
  14.  
  15.     This program was developed from a program called SCRUB
  16.     on BDS "C" User Group disk "Utilities 2" (Volume 2 in
  17.     the Software Tools RCPM BDSCAT.ALL).
  18. */
  19.  
  20. /*
  21.     Macros for constant definitions
  22. */
  23.  
  24. #include <STDIO.H>
  25.  
  26. #define VERSION 1    /* main version number */
  27. #define REVISION 1    /* sub version number */
  28. #define DEL 0x7F    /* ASCII delete character */
  29. #define WORKING 1024    /* number of chars between progress markers */
  30. #define NEXTLINE (WORKING * 32) /* number of progess chars on a screen line */
  31. #define CPMEOF 0x1A    /* CP/M-86 end of file marker */
  32. #define ERROR 0        /* Normal file error condition */
  33. #define FERROR -1    /* Flush file error */
  34.  
  35. /*
  36.     Argument vector indices
  37. */
  38.  
  39. #define FROM_FILE 1
  40. #define TO_FILE   2
  41.  
  42. /*
  43.     main to open the files for scrub()
  44.     and handle invocation errors.
  45. */
  46.  
  47. main(argc,argv)
  48. int argc;
  49. char *argv[];
  50.  
  51. {
  52.     char *fdin,*fdout;
  53.     char buf[12];
  54.  
  55.     printf("\nWordStar file Scrubber Version %d.%d\n",VERSION,REVISION);
  56.     printf("Bill Bolton, Software Tools\n");
  57.     if( argc != 3 )
  58.             usage();
  59.     else {
  60.         if( (fdin = fopen(argv[FROM_FILE],"r")) == ERROR){
  61.             printf("\nCannot find file %s\n",argv[FROM_FILE]);
  62.             usage();
  63.         }
  64.         else {
  65.             if( (fdout  = fopen(argv[TO_FILE],"w")) == ERROR )
  66.                 printf("\nCan't open %s\n",argv[TO_FILE]);
  67.             else {
  68.                 printf("\nWorking ");
  69.                 scrub(fdin,fdout);
  70.             }
  71.         }
  72.     }
  73.     exit();
  74. }
  75.  
  76. /*
  77.     procedure scrub -- copy file to file deleting unwanted control chars
  78. */
  79.  
  80. scrub(fdin,fdout)
  81. FILE *fdin;    /* the input file buffer */
  82. FILE *fdout;    /* the output file buffer */
  83.  
  84. {
  85.     int c;            /* 1 char buffer */
  86.     long count;        /* count of characters processed */
  87.     long killed;        /* numbers of bytes deleted */
  88.     long hyphen;        /* number of soft hyphens replaced */
  89.  
  90.     count  = 0;
  91.     killed = 0;
  92.     hyphen = 0;
  93.  
  94.     while( (c = getc(fdin)) != EOF  && c != CPMEOF ){
  95.         c &= 0x7F;
  96.         count++;
  97.         if (count % WORKING == 0)
  98.             printf("*");        /* still alive */
  99.         if (count % NEXTLINE == 0)
  100.             printf("\n\t");        /* new line every so often */
  101.         if( c >= ' ' && c < '\177' )    /* visable character ? */
  102.             putc(c,fdout);
  103.         else
  104.             switch(c) {
  105.                 case '\r':
  106.                 case '\n':
  107.                 case '\t':
  108.                     putc(c,fdout); /* ok control chars */
  109.                     break;
  110.  
  111.                 case '\037':    /* replace WS soft hyphen */
  112.                     putc('-',fdout);
  113.                     hyphen++;
  114.                     break;
  115.  
  116.                 default:
  117.                     killed++;
  118.                     break;         /* ignore it */
  119.               }
  120.     }
  121.     putc(CPMEOF,fdout);             /* sent textual end of file */
  122.     printf("\n");
  123.     if( fflush(fdout) == FERROR)
  124.         exit(puts("\nOutput file flush error\n"));
  125.     printf("\n%ld characters processed\n",count);
  126.     printf("%ld characters were deleted\n",killed);
  127.     printf("%ld soft hyphens replaced\n",hyphen);
  128. }
  129.  
  130. usage()
  131.  
  132. {
  133.         printf("\nUsage:\n\n");
  134.         printf("\tSCRUB d:file1 d:file2\n\n");
  135.         printf("Where:\n");
  136.         printf("\tfile1 = source file, (* and ? not allowed)\n");
  137.         printf("\tfile2 = destination file, (* and ? not allowed)\n");
  138.         printf("\td:    = optional drive identifier\n\n");
  139.         printf("i.e.\tSCRUB A:FOOBAR.WST B:FUBAR.DOC\n");
  140. }
  141.  
  142. /* end of scrub */
  143.